package com.bltec.emergency.controller;

import com.alibaba.fastjson.JSON;
import com.bltec.emergency.common.Constant;
import com.bltec.emergency.common.ResponseJson;
import com.bltec.emergency.model.Org;
import com.bltec.emergency.service.orgService.OrgService;
import com.bltec.emergency.service.mgrService.UserService;
import com.bltec.emergency.utils.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/org")
@RequiresPermissions("project:org")
public class OrgController {
    @Resource
    private OrgService orgService;
    @Resource
    private UserService userService;

    @RequestMapping("getAllOrg")
    public ResponseJson getAllOrg() {
        try {
            List<Org> org = orgService.getAll();
            return new ResponseJson(Constant.OK, JSON.toJSON(org));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseJson(Constant.FAIL, e.getMessage());
        }
    }

    /*         private String operate;*/
    @RequiresPermissions("org:insert")
    @RequestMapping("addOrg")
    public ResponseJson addOrg(String operate) {
        try {
            Org org = new Org();
            org.setOperate(operate);
            org.setEnable(true);
            org.setCreateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            org.setCreateTime(DateUtil.dateNow());
            orgService.add(org);
            return new ResponseJson(Constant.OK, "创建用户成功");
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, "创建用户失败" + e.getMessage());
        }
    }

    @RequiresPermissions("org:update")
    @RequestMapping("updateOrg")
    public ResponseJson updateOrg(Long id,String operate) {
        try {
            Org org = orgService.getOrgById(id);
            org.setOperate(operate);
            org.setEnable(true);
            org.setUpdateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            org.setUpdateTime(DateUtil.dateNow());
            orgService.updateOrgById(org);
            return new ResponseJson(Constant.OK, JSON.toJSON(org));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseJson.UPDATE_ERROR;
        }
    }
    @RequiresPermissions("org:delete")
    @RequestMapping("deleteOrg")
    public ResponseJson setDisable(Long id) {
        if (null != orgService.getOrgById(id)) {
            if (orgService.deleteOrgById(id)) {
                return new ResponseJson(Constant.OK, "删除成功");
            } else {
                return new ResponseJson(Constant.FAIL, "删除失败");
            }
        } else {
            return new ResponseJson(Constant.OK, "删除成功");
        }
    }
}
